//
//  RegisterRule2ViewController.m
//  GYMWinners
//
//  Created by Logic Solutions on 4/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterRule2ViewController.h"
#import "RegisterPayInfoViewController.h"

#import "BaseroutAPI.h"

@interface RegisterRule2ViewController ()

@end

@implementation RegisterRule2ViewController
@synthesize daysPickerView;
@synthesize penaltyPerTimeArr;

-(IBAction)nextStep{
    RegisterPayInfoViewController* payInfo = [[RegisterPayInfoViewController alloc] init];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
    
    [self.navigationController pushViewController:payInfo animated:YES];
    
    [UIView commitAnimations];
}

-(IBAction)back {
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = newBackButton; 

    //penaltyPerTimeArr= [[NSArray alloc]initWithObjects:@"5¥",@"10¥",@"15¥",@"20¥",@"30¥",@"40¥",@"60¥",@"80¥",@"100¥", nil];
    
    daysPickerView= [[UIPickerView alloc] initWithFrame:CGRectMake(0,110, 320, 240)];    
    daysPickerView.delegate=self;
    daysPickerView.showsSelectionIndicator=YES;
    [daysPickerView selectRow:0 inComponent:0 animated:YES]; //default selection
    [self updateStakeID:0];
    [self.view addSubview:daysPickerView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ //返回列数
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ //返回每列的最大行数
    NSArray *stakesArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"stakesArr"];
    return [stakesArr count];
}

//每一列中每一行的具体内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 
    
    NSArray *stakesArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"stakesArr"];
    NSDictionary *tmp = [stakesArr objectAtIndex:row];
    NSString *pickerRowTitle = [NSString stringWithFormat:@"%@¥", [tmp objectForKey:@"stakes"]];
    
    return pickerRowTitle;
}

- (void) updateStakeID:(NSInteger)row{
    NSArray *stakesArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"stakesArr"];
    NSDictionary *stake = [stakesArr objectAtIndex:row];
    NSString *stakeDollar = [NSString stringWithFormat:@"%@", [stake objectForKey:@"stakes"]];
    NSString *stakeID = [NSString stringWithFormat:@"%@", [stake objectForKey:@"id"]];
    
    NSLog(@"row=%d, stakeID=%@, stakeDollar=%@", row, stakeID, stakeDollar);
    
    [[BaseroutAPI sharedInstance] zSetVar:@"selectedStakeID" theValue:stakeID];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ //选中哪一列哪一行
    
    [self updateStakeID:row];

}

@end
